<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="../js/vue.js"></script>
    <title>计算属性</title>
</head>
<body>
    <!-- 计算属性
        getter当第一次加载的时候会调用一次，此时会有缓存，之后读取该数据值的时候，直接读取缓存
        修改的时候调用setter，缓存改变，它的改变来源于所依赖的响应式数据
    -->
    <div id="app">
        性：<input type="text" v-model="firstname"><br>
        名：<input type="text" v-model="lastname"><br>
        姓名(插值语法实现)：<b>{{firstname}}-{{lastname}}</b><br>
        姓名(methods方法实现)：<b>{{ name() }}</b> <br>
        姓名(computed方法实现)：<b>{{ fullname }}-----简写方式：{{fullname_simple}}</b>
    </div>
    <script>
        const vm = new Vue({
            data(){
                return{
                    firstname:'张',
                    lastname:'三',
                }
            },
            methods: {
                name(){
                    let {firstname,lastname} = this
                    return `${firstname}-${lastname}`
                }
            },
            computed:{
                fullname:{
                    get(){
                        let {firstname,lastname} = this
                        return `${firstname}-${lastname}`
                    },
                    set(val){
                        let a = val.split('-')[0]
                        let b = val.split('-')[1]
                        this.firstname = a; this.lastname = b
                    }
                },
                // 计算属性简写方式
                fullname_simple(){
                    let {firstname,lastname} = this
                    return `${firstname}-${lastname}`
                }
            }
        }).$mount("#app")
    </script>
</body>
</html>